package com.blocks.script;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;

import com.blocks.ScriptBlocksApplication;
import com.blocks.blocks.Block;
import com.blocks.blocks.MasterBlock;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

public class ScriptUtils {

	public static String writeSceneToScript(String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append("////TIME" + System.currentTimeMillis() + "\n");
		sb.append("importPackage(com.blocks.blocks);\n");
		sb.append("importPackage(com.blocks);\n");
		sb.append("importPackage(com.blocks.model);\n");
		sb.append("importPackage(com.jme3.math);\n");
		sb.append("importPackage(java.lang);\n");
		sb.append("var application = new Object();\n");
		sb.append("application.setup = function() {\n");
		Map<Long, MasterBlock> masterBlockMap = ScriptBlocksApplication.getInstance().getMasterBlockMap();
		try {
			for(Long masterblockId : masterBlockMap.keySet()){
				sb.append(writeClusterToString(masterblockId));
				sb.append("\n");
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		sb.append("}\n");
		sb.append("application.loop = function() {\n");
		sb.append("}\n");
		

		
		
		  try{
			    // Create file
			  if(fileName == null){
				  fileName = ScriptBlocksApplication.CONFIG_FILE;
			  }
			  File file = new File(ScriptBlocksApplication.getInstance().getScriptDir()
					  +ScriptBlocksApplication.getInstance().CONFIG_FILE_FOLDER+"/"+fileName);
			  System.out.println("Writing FILE " +  file.getCanonicalFile());
			    FileWriter fstream = new FileWriter(file);
			        BufferedWriter out = new BufferedWriter(fstream);
			    out.write(sb.toString());
			    //Close the output stream
			    out.close();
			    }catch (Exception e){//Catch exception if any
			      System.err.println("Could not save Script" + e.getMessage());
			    }
		
		
		return sb.toString();
		
		
	}

	
	private static String writeClusterToString(Long masterblockId){

		StringBuffer sb = new StringBuffer();
		MasterBlock masterBlock = ScriptBlocksApplication.getInstance().getMasterBlock(masterblockId);
		String blockVariable = masterBlock.getExportVariable();
		
		sb.append(masterBlock.scriptExport());
		sb.append("ScriptBlocksApplication.getInstance().getRootNode().attachChild("+blockVariable+");\n");			
		sb.append(writeChildren(masterBlock, blockVariable));
		sb.append(blockVariable+".updatePower("+masterBlock.isPowered()+");\n");
		sb.append("ScriptBlocksApplication.getInstance().addMasterBlock("+blockVariable+".getMasterId(), "+blockVariable+");\n");
		return sb.toString();
	}
	public static String writeChildren(Block block, String blockVariable ){
		StringBuffer sb = new StringBuffer();
		
		
		  for (Spatial spatial : block.getChildren()) {
			if (Block.class.isAssignableFrom(spatial.getClass())) {
				Block childBlock = (Block)spatial;
				String childBlockVariable = childBlock.getExportVariable();
				sb.append(childBlock.scriptExport());
				sb.append("\t"+blockVariable+".attachChild("+childBlockVariable+");\n");

				sb.append(writeChildren(childBlock, childBlockVariable));
				}
			}
		 
		
		return sb.toString();
	}
	
	public static boolean isBlock(Spatial spatial){
		if (Block.class.isAssignableFrom(spatial.getClass())) {
			return true;
		}
		return false;
	}
	public static String getTab(int tabLevel){
		final String TAB = "\t";
		StringBuffer tab = new StringBuffer();
		for(int x=0; x<tabLevel ;x++){
			tab.append(TAB);
		}
		return tab.toString();
	}
	
	public static int countNumberofBlocks(){
		int blockCount = 0;
		for(Long masterId :ScriptBlocksApplication.getInstance().getMasterBlockMap().keySet()){
			MasterBlock tempMaster = ScriptBlocksApplication.getInstance().getMasterBlock(masterId);
			blockCount = blockCount+tempMaster.getChildCount();
			blockCount++;
		}
		
		return blockCount;
		
	}
}
